@extends('layouts.app')
@section('content')
    <div class="breadcrumbs ace-save-state" id="breadcrumbs">
        <ul class="breadcrumb">
            <li>
                <i class="ace-icon fa fa-users"></i>
                <a href="{{url('user')}}">管理员管理</a>
            </li>
            <li>
                <a href="{{url('user/role')}}">角色列表</a>
            </li>
            <li class="active">分配行为</li>
        </ul><!-- /.breadcrumb -->
    </div>
    <div class="page-content" id="content">
        <div class="page-header">
            <h1>
                分配行为
                <small>
                    <i class="ace-icon fa fa-angle-double-right"></i>
                    赋予角色操作的权限
                </small>
            </h1>
        </div><!-- /.page-header -->
        @include('layouts.alert')
        <div class="row">
            <div class="col-sm-12">
                <form action="" class="form-horizontal" role="form">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 角色名称: </label>
                        <div class="col-sm-9">
                            <input type="text" value="{{$info->name}}" name="name" v-model="name" class="col-xs-10 col-sm-5" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 分配行为: </label>
                        <div class="col-sm-9">
                            <el-transfer
                                    v-model="value1"
                                    :titles="['所有行为', '拥有行为']"
                                    :button-texts="['取消', '添加']"
                                    filterable
                                    :filter-method="filterMethod"
                                    filter-placeholder="请输入行为关键字"
                                    :data="data"></el-transfer>
                        </div>
                    </div>
                    <div class="clearfix form-actions">
                        <div class="col-md-offset-3 col-md-9">
                            <button class="btn btn-info" type="button" @click="handleSubmit">
                                <i class="ace-icon fa fa-check bigger-110"></i>
                                提交
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection
@section('js')
    <script type="text/javascript">
        const app = new Vue({
            el: '#content',
            data: {
                name: '',
                data: [],
                value1: [],
                filterMethod(query, item){
                    return item.label.indexOf(query) > -1
                }
            },
            created(){
                const me = this;
                this.name = $("input[name='name']").val();
                $.post("{{url('user/getAllPermission')}}",{}, function (res) {
                    if (res.state === 'success'){
                        me.data = res.data
                    }else{
                        me.$message.error('获取所有行为失败,请刷新页面重试')
                    }
                });
                $.post("{{url('user/getAllPermission')}}",{role_id: "{{app('request')->id}}"}, function (res) {
                    if (res.state === 'success'){
                        let value1 = [];
                        res.data.forEach(element => {
                            value1.push(element.id)
                        });
                        me.value1 = value1
                    }else{
                        me.$message.error('获取角色拥有的行为失败,请刷新页面重试')
                    }
                })
            },
            methods: {
                handleSubmit() {
                    if (this.name.trim().length === 0){
                        this.$message.error('角色名称不能为空');
                        return;
                    }
                    const me = this;
                    $.post("{{route('editRole',['id' => app('request')->id])}}",{name:this.name,permission: this.value1.toString()}, function (res) {
                        if (res.state === 'success') {
                            me.$message.success(res.message)
                        }else{
                            me.$message.error(res.message)
                        }
                    })
                }
            }
        })
    </script>
@endsection